package lrg10002.plugins.akperms;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.plugin.java.JavaPlugin;

public class AKPermsPlugin extends JavaPlugin {

        public PermsLoader loader;
        public LoggingRecord currentRecord;
        public Logger logger = Logger.getLogger("Minecraft");
        public static AKPermsPlugin instance;

        @Override
        public void onEnable() {
                currentRecord = new LoggingRecord("startup");
                File perms = new File(this.getDataFolder(), "permissions.xml");
                if (!perms.exists()) {
                        try {
                                perms.createNewFile();
                                writeTemplateFile(perms);
                                currentRecord.addMessage("Created permissions.xml in plugins/AKPermissions.");
                        } catch (Exception e) {
                                currentRecord.addProblem("Could not create the missing permissions file.");
                                currentRecord.printLog(logger);
                                this.getPluginLoader().disablePlugin(this);
                        }

                }
                loader = new PermsLoader(this, perms);

                try {
                        loader.load();
                } catch (Exception e) {
                        loader.log.addProblem(e.getMessage());
                        currentRecord.addProblem("Could not load permissions.");
                        currentRecord.printLog(logger);
                        this.getPluginLoader().disablePlugin(this);
                }finally {
                         loader.log.printLog(logger);
                }
                

                currentRecord.printLog(logger);
        }

        @Override
        public void onDisable() {
                currentRecord = new LoggingRecord("shutdown");

                currentRecord.printLog(logger);
        }

        
        
        
        
        private void writeTemplateFile(File f) {
                try {
                        ArrayList<String> lines = new ArrayList<String>();
                        BufferedReader br = new BufferedReader(new InputStreamReader(AKPermsPlugin.class.getResourceAsStream("/template.xml")));
                        String line = "";
                        while ((line = br.readLine()) != null) {
                                if (line.contains("!!!!ADMIN!!!!")) {
                                        for (OfflinePlayer p : Bukkit.getOfflinePlayers()) {
                                                if (p.isOp()) {
                                                        line = line.replace("!!!!ADMIN!!!!", p.getName());
                                                        break;
                                                }
                                        }

                                        if (line.contains("!!!!ADMIN!!!!")) {
                                                line = line.replace("!!!!ADMIN!!!!", "Chezzy_Monkee_69");
                                        }

                                }
                                lines.add(line);
                        }

                        PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(f)));
                        for (String l : lines) {
                                pw.println(l);
                        }

                        pw.close();


                } catch (Exception e) {
                        currentRecord.addMessage("Could not write template to permissions file. NBD.");
                }
        }
}
